package j2024.j202410;

import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class j1028 {
    /**
     * 82. 删除排序链表中的重复元素 II
     * 给定一个已排序的链表的头 head ， 删除原始链表中所有重复数字的节点，只留下不同的数字 。返回 已排序的链表 。
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode ret = new ListNode(-1);
        ret.next = head;
        ListNode cur = ret;
        while(cur.next!=null && cur.next.next!=null){
            if(cur.next.val==cur.next.next.val){
                int x = cur.next.val;
                while(cur.next!=null && cur.next.val==x){
                    cur.next = cur.next.next;
                }
            }else {
                cur = cur.next;
            }
        }
        return ret.next;
    }

    public ListNode mergeKLists (ArrayList<ListNode> lists) {
        // write code here
        ListNode ret = new ListNode(-1);
        PriorityQueue<ListNode> q = new PriorityQueue<>((a,b)->{
            return a.val-b.val;
        });
        for (int i = 0; i < lists.size(); i++) {
           if(lists.get(i)!=null){
               q.offer(lists.get(i));
           }
        }
        ListNode cur = ret;
        while(!q.isEmpty()){
            ListNode tmp = q.poll();
            cur.next = tmp;
            cur = cur.next;
            if(tmp.next!=null){
                q.offer(tmp.next);
            }
        }
        return ret.next;
    }


    public static int[] dx = {0,0,-1,1};
    public static int[] dy = {1,-1,0,0};
    public static int ret = 1,n,m;
    public static int[][] arr;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();m = in.nextInt();
        arr = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                DFS(i,j,1);
            }
        }
        System.out.println(ret);
    }
    public  static void DFS(int i,int j,int cou){
        for (int k = 0; k < 4; k++) {
            int x = i+dx[k],y = j+dy[k];
            if(x>=0 && x<n && y>=0 && y<m && arr[x][y]>arr[i][j]){
                ret = Math.max(cou+1,ret);
                DFS(x,y,cou+1);
            }
        }
    }
}
